package cn.kgc.ssm.util;

import cn.kgc.ssm.enums.CodeMsg;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * @author 课工场
 * @date 2023/10/19
 * @description   请求结果的统一响应格式  layUi
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)   // 转换json格式数据时  只包含不为null的属性
public class ResponseResult {

    private String msg;
    private Integer code;   //  0  表示请求成功
    private  Long count;   // 分页数据显示的总条数

    // 属性名字 不要写成 date
    private Object data;   //  当前页显示的数据


    // 成功  没有数据
    public static  ResponseResult  success(){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(CodeMsg.SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.SUCCESS.getMsg());
        return  responseResult;
    }

    public static  ResponseResult  success(CodeMsg codeMsg){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(codeMsg.getCode());
        responseResult.setMsg(codeMsg.getMsg());
        return  responseResult;
    }


    public static  ResponseResult  success(String msg,Integer code){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(code);
        responseResult.setMsg(msg);
        return  responseResult;
    }

    // 成功  有数据   查询
    public static  ResponseResult  success(Object data,Long count){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(CodeMsg.SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.SUCCESS.getMsg());
        responseResult.setCount(count);
        responseResult.setData(data);
        return  responseResult;
    }

    // 失败  没有数据
    public static  ResponseResult  fail(){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(CodeMsg.FAIL.getCode());
        responseResult.setMsg(CodeMsg.FAIL.getMsg());
        return  responseResult;
    }

    public static  ResponseResult  fail(CodeMsg codeMsg){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(codeMsg.getCode());
        responseResult.setMsg(codeMsg.getMsg());
        return  responseResult;
    }

}
